package main

import (
	"fmt"
	"goHelpers/app/entity"
	"goHelpers/app/http/middleware"
	"net/http"

	myFormatsHelper "goHelpers/helper/formats"

	curlHelper "gitee.com/copylian/lian-go.git/helper/curl"
	formatsHelper "gitee.com/copylian/lian-go.git/helper/formats"
	hashHelper "gitee.com/copylian/lian-go.git/helper/hash"
	mapsHelper "gitee.com/copylian/lian-go.git/helper/maps"
	numberHelper "gitee.com/copylian/lian-go.git/helper/number"
	slicesHelper "gitee.com/copylian/lian-go.git/helper/slices"
	stringsHelper "gitee.com/copylian/lian-go.git/helper/strings"
	timeHelper "gitee.com/copylian/lian-go.git/helper/time"

	"github.com/gin-gonic/gin"
)

func init() {
	// 路由
	r := gin.Default()

	// 默认路由
	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "Hello GoHelpers!!!",
		})
	})

	// 切片
	r.GET("/slices", func(c *gin.Context) {
		// 获取下标，查不到返回-1
		indexSlices := slicesHelper.Index([]string{"a", "b", "c"}, "c")

		// 检测是否包含
		containsSlices := slicesHelper.Contains([]int{1, 2, 3}, 2)

		// 检测是否存在
		isInSlices := slicesHelper.In([]int{1, 2, 3}, 2)

		// 差集: 参数1在参数2不存在的值
		diffSlices := slicesHelper.Diff([]int{1, 2, 3}, []int{3, 4, 5, 6})

		// 去重
		uniqueSlices := slicesHelper.Unique([]string{"1", "2", "3", "4", "5", "4", "5"})

		// 合并，第三个参数：true-去重
		mergeSlices := slicesHelper.Merge([]int{1, 2, 3}, []int{2, 3, 4}, true)

		// 转字符串
		toStringSlices := slicesHelper.ToString([]uint8{1, 2, 3}, ",")

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "切片辅助函数",
			"data": map[string]interface{}{
				"1-indexSlices":    fmt.Sprintf("slicesHelper.Index() 查找下标：%v", indexSlices),
				"2-containsSlices": fmt.Sprintf("slicesHelper.In() 检测是包含：%v", containsSlices),
				"3-isInSlices":     fmt.Sprintf("slicesHelper.In() 检测是否存在：%v", isInSlices),
				"4-diffSlices":     fmt.Sprintf("slicesHelper.Diff() 差集：%v", diffSlices),
				"5-uniqueSlices":   fmt.Sprintf("slicesHelper.Unique() 去重：%v", uniqueSlices),
				"6-mergeSlices":    fmt.Sprintf("slicesHelper.Merge() 合并：%v", mergeSlices),
				"7-toStringSlices": fmt.Sprintf("slicesHelper.ToString() 转字符串：%v", toStringSlices),
			},
		})

	})

	// 数字
	r.GET("/number", func(c *gin.Context) {

		// 获取随机数字
		randNum := numberHelper.GetRandNum(10000)

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "数字辅助函数",
			"data": map[string]interface{}{
				"1-randNum": fmt.Sprintf("numberHelper.GetRandNum() 获取随机数字：%v", randNum),
			},
		})
	})

	// 字符串
	r.GET("/strings", func(c *gin.Context) {
		// base64编码
		base64EncodeStr := stringsHelper.Base64Encode("admin123456")

		// base64解码
		base64DecodeStr := stringsHelper.Base64Decode(base64EncodeStr)

		// 获取随机字符串
		randStr := stringsHelper.GetRand(6)

		// 生成密码
		passwordStr := stringsHelper.CreatePassword("admin123456", randStr)

		// 字符串转bytes
		strToBytes := stringsHelper.StrToBytes("admin123456")

		// bytes转string
		bytesToStr := stringsHelper.BytesToStr(strToBytes)

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "字符串辅助函数",
			"data": map[string]interface{}{
				"1-base64EncodeStr": fmt.Sprintf("stringsHelper.Base64Encode() base64编码：%v", base64EncodeStr),
				"2-base64DecodeStr": fmt.Sprintf("stringsHelper.Base64Decode() base64解码：%v", base64DecodeStr),
				"3-randStr":         fmt.Sprintf("stringsHelper.GetRand() 获取随机数：%v", randStr),
				"4-passwordStr":     fmt.Sprintf("stringsHelper.CreatePassword() 生成密码：%v", passwordStr),
				"5-strToBytes":      fmt.Sprintf("stringsHelper.StrToBytes() 字符串转bytes：%v", strToBytes),
				"6-bytesToStr":      fmt.Sprintf("stringsHelper.BytesToStr() bytes转string：%v", bytesToStr),
			},
		})
	})

	// hash
	r.GET("/hash", func(c *gin.Context) {
		// md5
		md5Hash := hashHelper.MD5("admin123456")

		// sha1
		sha1Hash := hashHelper.SHA1("admin123456")

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "hash辅助函数",
			"data": map[string]interface{}{
				"1-md5Hash":  fmt.Sprintf("hashHelper.MD5() md5加密：%v", md5Hash),
				"2-sha1Hash": fmt.Sprintf("hashHelper.SHA1() md5解密：%v", sha1Hash),
			},
		})
	})

	// 格式化
	r.GET("/formats", func(c *gin.Context) {
		// 打印格式化方法
		formatsHelper.P([]int{1, 2, 3, 4})
		formatsHelper.P(map[string]interface{}{
			"name": "只袄早",
			"sex":  "未知",
			"age":  28,
		})

		// 验证手机号
		isMobile := formatsHelper.IsMobile("15859279220")

		// 验证邮箱
		isEmail := formatsHelper.IsEmail("copylian@aikehou.com")

		// 验证是否URL
		isUrl := formatsHelper.IsUrl("https://www.copylian.com/?a=b&ss=bb")

		// 手机号加 *
		formatMobile := formatsHelper.FormatMobile("15859279220")

		// 递归
		data := []entity.RecursiveList{}
		for i := 1; i <= 5; i++ {
			data = append(data, entity.RecursiveList{
				Id:   uint32(i),
				Name: fmt.Sprintf("a%v", i),
				Pid:  uint32(i - 1),
			})
		}
		rdata := myFormatsHelper.RecursiveList(data, 0, []uint32{1, 2, 3})
		formatsHelper.P(rdata)

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "格式化辅助函数",
			"data": map[string]interface{}{
				"1-P()":          fmt.Sprintf("formatsHelper.P() 格式化打印方法：%v", "formatsHelper.P([]int{1, 2, 3, 4})"),
				"2-isMobile":     fmt.Sprintf("formatsHelper.IsMobile() 验证手机号：%v", isMobile),
				"3-isEmail":      fmt.Sprintf("formatsHelper.IsEmail() 验证邮箱：%v", isEmail),
				"4-isUrl":        fmt.Sprintf("formatsHelper.IsUrl() 验证是否URL：%v", isUrl),
				"5-formatMobile": fmt.Sprintf("formatsHelper.FormatMobile() 手机号加 *：%v", formatMobile),
			},
		})
	})

	// 时间
	r.GET("/time", func(c *gin.Context) {
		// 时间模板
		layout := "2006-01-02 15:04:05"

		// 获取当前时间戳
		nowtime := timeHelper.Time()

		// 时间戳格式化
		timeToDate := timeHelper.TimeToDate(nowtime, layout)

		// 创建一个时间Time
		newDateTime := timeHelper.NewDateTime(timeToDate, layout)

		// 字符串转时间戳
		strToTime := timeHelper.StrToTime(timeToDate, layout)

		// rfc时间格式化成指定的格式
		rcToDate := timeHelper.RfcToLocalDatetime("2023-04-20T12:41:45.14Z", "")

		// rfc时间格式化成时间戳
		rcToTime := timeHelper.RfcToLocalTime("2023-04-20T12:41:45.14Z", "")

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "时间辅助函数",
			"data": map[string]interface{}{
				"1-nowtime":     fmt.Sprintf("timeHelper.Time() 获取当前时间戳：%v", nowtime),
				"2-timeToDate":  fmt.Sprintf("timeHelper.TimeToDate() 时间戳格式化：%v", timeToDate),
				"3-newDateTime": fmt.Sprintf("timeHelper.NewDateTime() 创建一个时间Time：%v", newDateTime),
				"4-strToTime":   fmt.Sprintf("timeHelper.StrToTime() 字符串转时间戳：%v", strToTime),
				"5-rcToDate":    fmt.Sprintf("timeHelper.RfcToLocalDatetime() rfc时间格式化成指定的格式：%v", rcToDate),
				"6-rcToTime":    fmt.Sprintf("timeHelper.RfcToLocalTime() rfc时间格式化成时间戳：%v", rcToTime),
			},
		})
	})

	// map
	r.GET("/maps", func(c *gin.Context) {

		// 获取map的键
		mapData := map[string]string{
			"name":     "只袄早",
			"sex":      "未知",
			"email":    "copylian@aikehou.com",
			"age":      "28",
			"province": "福建",
			"city":     "厦门",
		}
		keys := mapsHelper.Keys(mapData)

		// 获取map的值
		values := mapsHelper.Values(mapData)

		// 判断map是否相同
		mapData2 := map[string]string{
			"name":     "只袄早",
			"sex":      "未知",
			"email":    "copylian@aikehou.com",
			"age":      "28",
			"province": "福建",
			"city":     "厦门",
		}
		isSame := mapsHelper.Equal(mapData, mapData2)
		isSame2 := mapsHelper.EqualFunc(mapData, mapData2, func(v1, v2 string) bool {
			return v1 == v2
		})

		// 清空
		mapsHelper.Clear(mapData2)

		// 克隆
		mapClone := mapsHelper.Clone(mapData)

		// 复制
		mapCopy := map[string]string{}
		mapsHelper.Copy(mapCopy, mapData)
		fmt.Printf("mapCopy: %v\n", mapCopy)

		// 通过函数比对之后删除
		mapData3 := map[string]string{
			"name":     "只袄早",
			"sex":      "未知",
			"email":    "copylian@aikehou.com",
			"age":      "28",
			"province": "福建",
			"city":     "厦门",
		}
		mapsHelper.DeleteFunc(mapData3, func(k, v string) bool {
			return k == "sex"
		})

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "map辅助函数",
			"data": map[string]interface{}{
				"1-keys":       fmt.Sprintf("mapsHelper.Keys() 获取map的键：%v", keys),
				"2-values":     fmt.Sprintf("mapsHelper.Values() 获取map的值：%v", values),
				"3-isSame":     fmt.Sprintf("mapsHelper.Equal() 判断map是否相同：%v", isSame),
				"4-isSame2":    fmt.Sprintf("mapsHelper.EqualFunc() 判断map是否相同：%v", isSame2),
				"5-clear":      fmt.Sprintf("mapsHelper.Clear() 清空：%v", mapData2),
				"6-mapClone":   fmt.Sprintf("mapsHelper.Clone() 克隆：%v", mapClone),
				"7-mapCopy":    fmt.Sprintf("mapsHelper.Copy() 复制：%v", mapCopy),
				"8-deleteFunc": fmt.Sprintf("mapsHelper.DeleteFunc() 通过函数比对之后删除：%v", mapData3),
			},
		})
	})

	// curl
	r.GET("/curl", func(c *gin.Context) {
		// curl请求
		curlData, _ := curlHelper.Curl(curlHelper.Request{
			Url:    "https://www.baidu.com/",
			Method: "GET",
			Headers: map[string]string{
				"token": "token123",
			},
			BodyData:    "123",
			ReqTimeOut:  10,
			RespTimeOut: 10,
		})
		formatsHelper.P(curlData)

		// get请求
		getData, _ := curlHelper.Get(curlHelper.Request{
			Url: "https://www.baidu.com/",
			Headers: map[string]string{
				"token": "token123",
			},
			BodyData:    "123",
			ReqTimeOut:  10,
			RespTimeOut: 10,
		})
		formatsHelper.P(getData)

		// post请求
		postData, _ := curlHelper.Get(curlHelper.Request{
			Url: "https://www.baidu.com/",
			Headers: map[string]string{
				"token": "token123",
			},
			BodyData:    "123",
			ReqTimeOut:  10,
			RespTimeOut: 10,
		})
		formatsHelper.P(postData)

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "curl辅助函数",
			"data": map[string]interface{}{
				"1-curlData": fmt.Sprintf("curlHelper.Curl() Curl请求：%v", curlData),
				"2-getData":  fmt.Sprintf("mapsHelper.Get() Get请求：%v", getData),
				"3-postData": fmt.Sprintf("mapsHelper.Post() Post请求：%v", postData),
			},
		})
	})

	// jwt-auth 中间件
	r.Use(middleware.ParseUserWithErr()).GET("/jwt-auth", func(c *gin.Context) {
		// 获取jwt用户ID
		user_id, _ := c.Get("user_id")

		// 返回
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "jwt-auth 中间件请求",
			"data": map[string]interface{}{
				"1-user_id": fmt.Sprintf("获取jwt用户ID：%v", user_id),
			},
		})
	})

	// 启动服务
	r.Run(":9999")
}
